package com.chengn.policy;

import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;



public class PolicyTest {
    public static void main(String[] args) {

        /**
         *     - AbortPolicy 策略：直接抛出异常，阻止系统正常工作
         *     - CallerRunsPolicy 策略：如果线程池的线程数量达到上限，该策略会把任务队列中的任务放在调用者线程当中运行。
         *     - DiscardOldestPolicy ：丢弃最先被添加进队列的任务，并尝试重新提交
         *     - DiscardPolicy ：丢弃无法处理的任务，不予处理。
         */
        ThreadPoolExecutor executor = new ThreadPoolExecutor(1,
                1,
                1, TimeUnit.SECONDS,
                new SynchronousQueue<>(),
                new ThreadPoolExecutor.AbortPolicy());


        for (int i = 0; i < 5; ++i) {
            int j = i;
            executor.execute(() -> {
                System.out.println(j + " " +Thread.currentThread().getName());
            });
        }
    }

}
